package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DelPicUtil;
import com.guiji.quartz.vo.CountVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: wangxw
 * @date 2021/12/31 11:16
 */
public class Gamespot {
	private static final Logger log = LoggerFactory.getLogger(Gamespot.class);
	private static final String host = "https://www.gamespot.com";
	private static final String baseUrl = "https://www.gamespot.com/news/";
	private static final boolean needProxy = false;

	static {
		System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
	}

	public static void mainMethod() throws Exception {

		HttpResponse response = null;
		try {
			HttpRequest request = HttpRequest.get(baseUrl)
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
			if (needProxy) {
				request = request.setHttpProxy(ProxyConfig.url, ProxyConfig.ip);
			}
			response = request.setConnectionTimeout(30000).execute();

			if (null != response && response.getStatus() == 200) {
				List<HttpCookie> cookies = response.getCookies();
				String resp = response.body();
				Set<String> resUrl = getUrl(resp);
				getContent(resUrl, cookies);
			} else {
				log.error(baseUrl + String.valueOf(response.getStatus()));
			}

		} catch (Exception e) {
			log.error(String.format("访问 %s 失败", baseUrl));
			return;
		}
	}


	private static void getContent(Set<String> listContent, List<HttpCookie> cookies) throws MalformedURLException, URISyntaxException, InterruptedException {
		List<Map> allNews = new ArrayList();
		for (String url : listContent) {
			Thread.sleep(1000);
			if (!url.startsWith("https")) {
				url = new StringBuffer(host).append(url).toString();
			}
			if (null != cookies && cookies.size() > 0) {
				CookieManager ccm = new CookieManager();
				CookieStore CookieStore = ccm.getCookieStore();
				for (HttpCookie cookie : cookies) {
					URL domain = new URL(host);
					CookieStore.add(domain.toURI(), cookie);
				}
				HttpRequest.setCookieManager(ccm);
			}

			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = null;
				HttpRequest request = HttpRequest.get(url)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
					.header("Refer", baseUrl);
				if (needProxy) {
					request = request.setHttpProxy(ProxyConfig.url, ProxyConfig.ip);
				}

				response = request.setConnectionTimeout(30000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1//text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			int a = 0, b = 0;
			try {
				String upvote = (String) jxDocument.sel("//button[@id='upvote_upvote']/span/text()").get(0);
				a = Integer.parseInt(ReUtil.findAll("[0-9]{1,10}", upvote, 0).get(0));
				String comment = (String) jxDocument.sel("//div[@class='content-footer__user-actions']/a/text()").get(0);
				b = Integer.parseInt(ReUtil.findAll("[0-9]{1,10}", comment, 0).get(0));
			} catch (Exception e) {
			}
			StringBuffer content = new StringBuffer();
			String contentStr = null;
			JXDocument jxDocumentP;
			Document document = null;
			String temp = null;
			try {

				document = Jsoup.parse(doc);
				Element mainArea = document.getElementsByClass("js-content-entity-body content-entity-body").get(0);
				mainArea.select("iframe").remove();
				mainArea.select("figcaption").remove();
				mainArea.select("div").remove();
				contentStr = mainArea.toString()
					.replaceAll("<br>", "\\$\\$\\$")
					.replaceAll("</p>", "\\$\\$\\$")
					.replaceAll("</ul>.*?\n", "\\$\\$\\$")
					.replaceAll("</li>.*?\n", "\\$\\$\\$")
					.replaceAll("&nbsp;", " ")
					.replaceAll("\n", "")
					.replaceAll("<(.|\n)*?>", " ");

			} catch (Exception e) {
				continue;
			}

			List<Object> srcImgs = new ArrayList<>();
			List<String> newImgs = new ArrayList<>();
			String img = null;
			try {

				String imgXpath = "//meta[@property='og:image']/@content";
				int imgSize = jxDocument.sel(imgXpath).size();
				if (imgSize == 0) {
					imgXpath = "//figure[@data-size='large']//img/@src";
					imgSize = jxDocument.sel(imgXpath).size();

				}
				if (imgSize == 0) {
					imgXpath = "//meta[@property='og:image']/@content";
					imgSize = jxDocument.sel(imgXpath).size();
				}

				if (imgSize > 0) {
					for (int k = 0; k < imgSize; k++) {
						temp = jxDocument.sel(imgXpath).get(k).toString();
						img = temp.strip();
						if (img.contains(" ")) {
							img = img.substring(0, img.indexOf(" "));
						}
						srcImgs.add(img);
						break;
					}
				}

			} catch (Exception e) {
				log.error("可能没有图片");
				img = "";
			} finally {
				if (srcImgs.size() > 0) {
					newImgs = DelPicUtil.delPic(srcImgs);
				}
			}

			Date date = new Date();
			if (contentStr.length() > 50000 || contentStr.replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
				continue;
			}

			SpiderNews spiderNews = new SpiderNews();
			spiderNews.setNewsTitle(title);
			spiderNews.setNewsContent(contentStr);
			spiderNews.setNewsPicJson(JSONUtil.toJsonStr(newImgs));
			spiderNews.setNewsUrl(url);
			spiderNews.setNewsPublicTime(date);
			HashMap map = new HashMap();
			map.put("spidernews", spiderNews);
			map.put("hotData", a + b);
			allNews.add(map);
		}

		//对新闻热度排序
		Collections.sort(allNews, (o1, o2) -> (int) o2.get("hotData") - (int) o1.get("hotData"));
		int k = 10;
		if (allNews.size() < 10) {
			k = allNews.size();
		}
		for (int i = 0; i < k; i++) {
			Map map1 = allNews.get(i);
			SpiderNews sp = (SpiderNews) map1.get("spidernews");
			try {
				DataSaveUtil.saveData(sp.getNewsTitle(), sp.getNewsContent(), sp.getNewsPicJson(), sp.getNewsUrl(), sp.getNewsPublicTime(), host, "gamespot-ACGnews");
				log.info(Thread.currentThread().getName() + "-----------------gamespot-ACGnews----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static Set<String> getUrl(String content) {
		Set<String> res = new HashSet<>();


		JXDocument jxDocument = new JXDocument(content);
		int imgSize = 0;
		try {
			imgSize = jxDocument.sel("//a/@href").size();
			if (imgSize > 0) {

				for (int k = 0; k < imgSize; k++) {
					String temp = null;
					temp = jxDocument.sel("//a/@href").get(k).toString();
					temp = temp.strip();
					if (temp.contains("-") && temp.contains("/articles/")) {
						res.add(temp);

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return res;
	}


//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}

}




